package com.example.administrator.nl.activity.shop;

import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.nl.R;
import com.example.administrator.nl.activity.appointment.AppointmentProjectActivity;
import com.example.administrator.nl.base.BaseActivity;
import com.example.administrator.nl.dialog.DialogShareShoppingView;
import com.example.administrator.nl.dialog.DialogShoppingCartView;
import com.example.administrator.nl.util.ToastUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShoppingDetailsActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.webView)
    WebView webView;

    @BindView(R.id.iv_share)
    ImageView iv_share;

    // 立即购买
    @BindView(R.id.tv_submit_shopping)
    TextView tv_submit_shopping;

    DialogShoppingCartView dialogShoppingCartView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tv_submit_shopping.setOnClickListener(this);
        iv_share.setOnClickListener(this);
        showShareDialog();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_shopping_details;
    }

    @Override
    public void init() {
        webView.loadUrl("https://news.163.com/");

    }


    TextView tv_dialog_number;
    TextView tv_dialog_cart;
    TextView tv_dialog_shopping;
    ImageView iv_dialog_reduce;
    ImageView iv_dialog_plus;
    TagFlowLayout tabLayout;

    List<String> tabList;

    private void initDialog() {

        dialogShoppingCartView = new DialogShoppingCartView(ShoppingDetailsActivity.this, R.layout.dialog_shopping_cart, R.style.dialog);
        dialogShoppingCartView.show();
        tv_dialog_cart = dialogShoppingCartView.findViewById(R.id.tv_dialog_cart);
        tv_dialog_shopping = dialogShoppingCartView.findViewById(R.id.tv_dialog_shopping);
        iv_dialog_reduce = dialogShoppingCartView.findViewById(R.id.iv_dialog_reduce);
        iv_dialog_plus = dialogShoppingCartView.findViewById(R.id.iv_dialog_plus);
        tv_dialog_number = dialogShoppingCartView.findViewById(R.id.tv_dialog_number);
        tabLayout = dialogShoppingCartView.findViewById(R.id.tabLayout);
        tabList = new ArrayList<>();
        tabList.add("白色");
        tabList.add("黑色");
        // 最多选中一个
        tabLayout.setMaxSelectCount(1);
        tabLayout.setAdapter(new TagAdapter(tabList) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {
                TextView tv = (TextView) LayoutInflater.from(ShoppingDetailsActivity.this).inflate(R.layout.label_appointment_project,
                        null);
                tv.setText(o.toString());
                return tv;
            }
        });

        tv_dialog_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.ToastShort("点击了加入购物车");
                dialogShoppingCartView.dismiss();
            }
        });

        tv_dialog_shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.ToastShort("点击了立即购买");
                dialogShoppingCartView.dismiss();
            }
        });

        iv_dialog_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog_number > 0) {
                    dialog_number--;
                    tv_dialog_number.setText(dialog_number + "");
                }
            }
        });

        iv_dialog_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_number++;
                tv_dialog_number.setText(dialog_number + "");
            }
        });

    }

    private int dialog_number;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit_shopping:
                initDialog();
                dialog_number = 0;
                break;
            case R.id.iv_share:
                dialogShareShoppingView.show();
                break;
        }
    }

    DialogShareShoppingView dialogShareShoppingView;
    private int[] viewId = new int[]{R.id.iv_share_wx, R.id.iv_share_pyq, R.id.iv_share_qq, R.id.tv_cancel};

    private void showShareDialog() {
        dialogShareShoppingView = new DialogShareShoppingView(ShoppingDetailsActivity.this, R.layout.dalog_share_shopping_view, R.style.dialog, viewId);
        dialogShareShoppingView.setOnCenterItemClickListener(new DialogShareShoppingView.OnCenterItemClickListener() {
            @Override
            public void OnCenterItemClick(DialogShareShoppingView dialog, View view) {
                switch (view.getId()) {
                    case R.id.iv_share_wx:
                        ToastUtil.ToastShort("分享微信");
                        break;
                    case R.id.iv_share_pyq:
                        ToastUtil.ToastShort("分享朋友圈");
                        break;
                    case R.id.iv_share_qq:
                        ToastUtil.ToastShort("分享QQ");
                        break;
                    case R.id.tv_cancel:
                        ToastUtil.ToastShort("取消");
                        dialog.dismiss();
                        break;
                }
            }
        });
    }
}
